package ibmetp.mis.listener;

import ibmetp.mis.vo.User;

import java.util.HashMap;
import java.util.Map.Entry;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class OnlineListener implements ServletContextListener,ServletContextAttributeListener,HttpSessionListener,HttpSessionAttributeListener {
	private ServletContext application = null ; 
    private HttpSession session = null ;
	public void contextDestroyed(ServletContextEvent sce) {
		
		
	}

	public void contextInitialized(ServletContextEvent sce) {
		System.out.println("Creaet Application Online List");
		//初始化一个application对象 
        this.application=sce.getServletContext() ; 
        //设置一个列表属性，用于保存在线用户名 
        this.application.setAttribute("online", new HashMap()) ; 
		
	}

	public void attributeAdded(ServletContextAttributeEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void attributeRemoved(ServletContextAttributeEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void attributeReplaced(ServletContextAttributeEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void sessionCreated(HttpSessionEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void sessionDestroyed(HttpSessionEvent se) {
		  
		//取得用户名列表 
        HashMap map=(HashMap)this.application.getAttribute("online") ; 
        //取得当前用户名 
        User user=(User)se.getSession().getAttribute("userinfo") ; 
      
        //将此用户名从列表中删除 
         map.remove(user.getUserNo()) ; 
        //将删除后的列表重新设置到application属性中 
        this.application.setAttribute("online", map) ;
        
		
	}

	public void attributeAdded(HttpSessionBindingEvent se) {
		//System.out.println("add Session attribute..");
		 //取得用户名列表 
        HashMap online=(HashMap)this.application.getAttribute("online") ; 
        //将当前用户名添加到列表中 
        //System.out.println("ADD SESSION ATTRIBUTE"+se.getName());
        User user=(User)se.getSession().getAttribute("userinfo");
        if(!online.containsKey(user.getUserNo()))
          online.put(user.getUserNo(),user.getUserName());
        //将添加后的列表重新设置到application属性中 
        this.application.setAttribute("online", online) ; 
        
        
	}

	public void attributeRemoved(HttpSessionBindingEvent be) {
		System.out.println("Attribute Removed"+be.getName()+be.getValue());
		//取得用户名列表 
        HashMap map=(HashMap)this.application.getAttribute("online") ; 
        //取得当前用户名 
        User user=(User)be.getValue() ; 
      
        //将此用户名从列表中删除 
         map.remove(user.getUserNo()) ; 
        //将删除后的列表重新设置到application属性中 
        this.application.setAttribute("online", map) ;
        
		
	}

	public void attributeReplaced(HttpSessionBindingEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	

}
